a tool for shared writing and social publishing
at debug/datetime 63 lines 1.8 kB view raw
1import { z } from "zod"; 2import { makeRoute } from "../lib"; 3import type { Env } from "./route"; 4import { AtUri } from "@atproto/syntax"; 5import { getFactsFromHomeLeaflets } from "./getFactsFromHomeLeaflets"; 6 7export type GetPublicationDataReturnType = Awaited< 8 ReturnType<(typeof get_publication_data)["handler"]> 9>; 10export const get_publication_data = makeRoute({ 11 route: "get_publication_data", 12 input: z.object({ 13 did: z.string(), 14 publication_name: z.string(), 15 }), 16 handler: async ( 17 { did, publication_name }, 18 { supabase }: Pick<Env, "supabase">, 19 ) => { 20 let uri; 21 if (/^(?!\.$|\.\.S)[A-Za-z0-9._:~-]{1,512}$/.test(publication_name)) { 22 uri = AtUri.make( 23 did, 24 "pub.leaflet.publication", 25 publication_name, 26 ).toString(); 27 } 28 let { data: publication, error } = await supabase 29 .from("publications") 30 .select( 31 `*, 32 documents_in_publications(documents( 33 *, 34 comments_on_documents(count), 35 document_mentions_in_bsky(count) 36 )), 37 publication_subscriptions(*, identities(bsky_profiles(*))), 38 publication_domains(*), 39 leaflets_in_publications(*, 40 documents(*), 41 permission_tokens(*, 42 permission_token_rights(*), 43 custom_domain_routes!custom_domain_routes_edit_permission_token_fkey(*) 44 ) 45 )`, 46 ) 47 .or(`name.eq."${publication_name}", uri.eq."${uri}"`) 48 .eq("identity_did", did) 49 .single(); 50 51 let leaflet_data = await getFactsFromHomeLeaflets.handler( 52 { 53 tokens: 54 publication?.leaflets_in_publications.map( 55 (l) => l.permission_tokens?.root_entity!, 56 ) || [], 57 }, 58 { supabase }, 59 ); 60 61 return { result: { publication, leaflet_data: leaflet_data.result } }; 62 }, 63});